package com.ioc.business.order.vo.receive.data.board;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.ioc.business.order.domain.ReceiveOrder;
import com.ioc.business.order.enums.OrderStatusEnum;
import com.ioc.common.constant.HttpStatus;
import com.ioc.common.core.page.TableDataInfo;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.utils.CollectionUtils;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class ReceiveDataBoardPageVo extends TableDataInfo {
    /**
     * 订单量
     **/
    private Integer orderCount;
    /**
     * 发货量
     **/
    private Integer receiveCount;
    /**
     * 待出库量
     **/
    private Integer waitingOutStorageCount;
    /**
     * 作业完成率
     **/
    private String jobCompletionRate;
    /**
     * 状态逾期
     **/
    private Integer statusDelayCount;

    public ReceiveDataBoardPageVo(TableVo tableVo) {
        setCode(HttpStatus.SUCCESS);
        setMsg("查询成功");
        setRows(tableVo.getVos());
        setTotal(new PageInfo(tableVo.getRows()).getTotal());
    }

    public ReceiveDataBoardPageVo(TableVo tableVo, List<ReceiveOrder> list) {
        this(tableVo);

        BigDecimal orderTotalCountBigDecimal = CollectionUtil.isNotEmpty(list) ?
                BigDecimal.valueOf(list.stream().map(ReceiveOrder::getOrderCode).distinct().count()) : BigDecimal.ZERO;
        this.orderCount = orderTotalCountBigDecimal.intValue();
        this.receiveCount = CollectionUtil.isNotEmpty(list) ?
                CollectionUtils.computeMapperTotal(list, ReceiveOrder::getMaterialQuantity).intValue() : 0;
        this.waitingOutStorageCount = CollectionUtil.isNotEmpty(list) ?
                CollectionUtils.computeMapperTotal(list.stream().filter(i ->
                                !OrderStatusEnum.FINISH.equals(i.getReceiveStatus())).collect(Collectors.toList()),
                        ReceiveOrder::getMaterialQuantity).intValue() : 0;
        //已完成订单数量
        BigDecimal finishCountBigDecimal = CollectionUtil.isNotEmpty(list) ?
                BigDecimal.valueOf(list.stream().filter(i -> OrderStatusEnum.FINISH.equals(i.getReceiveStatus()))
                        .map(ReceiveOrder::getOrderCode).distinct().count()) : BigDecimal.ZERO;
        //（已完成订单数量/总订单数量）*100%
        this.jobCompletionRate = !BigDecimal.ZERO.equals(orderTotalCountBigDecimal) ?
                finishCountBigDecimal.divide(orderTotalCountBigDecimal, 4, RoundingMode.HALF_UP)
                        .setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString() + "%" : "0%";
        this.statusDelayCount = CollectionUtil.isNotEmpty(list) ?
                CollectionUtils.computeMapperTotal(list.stream().filter(i ->
                                !OrderStatusEnum.OVERDUE.equals(i.getReceiveStatus())).collect(Collectors.toList()),
                        ReceiveOrder::getMaterialQuantity).intValue() : 0;
    }
}
